﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.CustomControls;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Form Variations
    /// </summary>
    public partial class FormVariations : Form, IFormFilter
    {
        protected Image m_ImageOriginal = null;
        protected SelectablePictureBox m_SelectablePictureBox = new SelectablePictureBox();
        protected Dictionary<SelectablePictureBox, UnsafeBitmap> aDict = new Dictionary<SelectablePictureBox, UnsafeBitmap>();

        protected Image m_ImageCurrentPick = null;

        protected byte m_nRedDiff = 10;
        protected byte m_nGreenDiff = 10;
        protected byte m_nBlueDiff = 10;

        static UnsafeBitmap bitmapMoreRed = null;
        static UnsafeBitmap bitmapMoreGreen = null;
        static UnsafeBitmap bitmapMoreBlue = null;

        static UnsafeBitmap bitmapMoreYellow = null;
        static UnsafeBitmap bitmapMoreCyan = null;
        static UnsafeBitmap bitmapMoreMagenta = null;

        static UnsafeBitmap bitmapLighter = null;
        static UnsafeBitmap bitmapDarker = null;

        /// <summary>
        /// Form Variations
        /// </summary>   
        public FormVariations()
        {
            InitializeComponent();
            pbOriginal.BackgroundImage = m_ImageOriginal;

            //red green blue
            pictureBoxMoreRed.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxMoreRed.Tag = bitmapMoreRed;
            pictureBoxMoreRed.DoubleClick += new EventHandler(pictureBox_DoubleClick);

            pictureBoxMoreBlue.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxMoreBlue.Tag = bitmapMoreBlue;
            pictureBoxLighter.DoubleClick += new EventHandler(pictureBox_DoubleClick);

            pictureBoxMoreGreen.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxMoreGreen.Tag = bitmapMoreGreen;
            pictureBoxMoreGreen.DoubleClick += new EventHandler(pictureBox_DoubleClick);
            
            //cyan yellow magenta
            pictureBoxMoreCyan.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxMoreCyan.Tag = bitmapMoreCyan;
            pictureBoxMoreCyan.DoubleClick += new EventHandler(pictureBox_DoubleClick);

            pictureBoxMoreMagenta.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxMoreMagenta.Tag = bitmapMoreMagenta;
            pictureBoxMoreMagenta.DoubleClick += new EventHandler(pictureBox_DoubleClick);

            pictureBoxMoreYellow.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxMoreYellow.Tag = bitmapMoreYellow;
            pictureBoxMoreYellow.DoubleClick += new EventHandler(pictureBox_DoubleClick);

            //lighter darker
            pictureBoxDarker.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxDarker.DoubleClick += new EventHandler(pictureBox_DoubleClick);
            pictureBoxDarker.Tag = bitmapDarker;

            pictureBoxLighter.GotFocus += new EventHandler(pictureBox_GotFocus);
            pictureBoxLighter.DoubleClick += new EventHandler(pictureBox_DoubleClick);
            pictureBoxLighter.Tag = bitmapLighter;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pictureBox_DoubleClick(object sender, EventArgs e)
        {
            MainFormSingleton.ActivePictureBox.BackgroundImage = (Bitmap)m_ImageCurrentPick.Clone();
            MainFormSingleton.ActivePictureBox.Invalidate();
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pictureBox_GotFocus(object sender, EventArgs e)
        {
            SelectablePictureBox selectablePictureBox = sender as SelectablePictureBox;
            UnsafeBitmap bitmap = (UnsafeBitmap)selectablePictureBox.Tag;
            m_ImageCurrentPick = bitmap.InternalBitmap;
            pictureBoxCurrentPickColor.BackgroundImage = m_ImageCurrentPick;
            pictureBoxCurrentPickLighterDarker.BackgroundImage = m_ImageCurrentPick;
            pbCurrentPick.BackgroundImage = m_ImageCurrentPick;
        }

        /// <summary>
        /// OnActivated
        /// </summary>
        /// <param name="e"></param>
        protected override void OnShown(EventArgs e)
        {
            m_ImageCurrentPick = (Bitmap)m_ImageOriginal.Clone();
            ShowAllVariations();
            base.OnShown(e);
        }

        /// <summary>
        /// Original Image
        /// </summary>
        public Image imgOriginal
        {
            get { return m_ImageOriginal; }
            set { m_ImageOriginal = value; }
        }

        /// <summary>
        /// We got no selectable picturebox here...
        /// </summary>
        public SelectablePictureBox PictureBox
        {
            get { return pbOriginal; }
        }

        /// <summary>
        /// All Variations
        /// </summary>
        /// <param name="bitmap"></param>
        private void AllVariations(UnsafeBitmap bitmap)
        {
            int y = 0;
            int x = 0;
            bitmapMoreRed = (UnsafeBitmap)bitmap.Clone();
            bitmapMoreGreen = (UnsafeBitmap)bitmap.Clone();
            bitmapMoreBlue = (UnsafeBitmap)bitmap.Clone();

            bitmapMoreYellow = (UnsafeBitmap)bitmap.Clone();
            bitmapMoreCyan = (UnsafeBitmap)bitmap.Clone(); ;
            bitmapMoreMagenta = (UnsafeBitmap)bitmap.Clone();
            
            bitmapLighter = (UnsafeBitmap)bitmap.Clone();
            bitmapDarker = (UnsafeBitmap)bitmap.Clone();

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            bool bShowClipping = chkbShowClipping.Checked;

            byte a = 0;
            byte r = 0;
            byte g = 0;
            byte b = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    bitmap.GetPixelDirect(x, y, out a, out r, out g, out b);

                    int nRed = r + m_nRedDiff;
                    int nBlue = b + m_nBlueDiff;
                    int nGreen = g + m_nGreenDiff;
                    
                    if (!bShowClipping)
                    {
                        nRed = (byte)(nRed > 255 ? ImageMath.Clamp(nRed, 0, 255) : nRed);
                        nGreen = (byte)(nGreen > 255 ? ImageMath.Clamp(nRed, 0, 255) : nGreen);
                        nBlue = (byte)(nBlue > 255 ? ImageMath.Clamp(nBlue, 0, 255) : nBlue);
                    }

                    bitmapMoreRed.SetPixelDirect(x, y, (byte)nRed , g, b); //more red)
                    bitmapMoreBlue.SetPixelDirect(x, y, r, g, (byte)nBlue); //more blue)
                    bitmapMoreGreen.SetPixelDirect(x, y, r, (byte)nGreen, b); //more green)

                    bitmapMoreMagenta.SetPixelDirect(x, y, (byte)nRed, g, (byte)nBlue); //more magenta
                    bitmapMoreCyan.SetPixelDirect(x, y, r, (byte)nGreen, (byte)nBlue); //more cyan
                    bitmapMoreYellow.SetPixelDirect(x, y, (byte)nRed, (byte)nGreen, b); //more yellow
                    
                    bitmapLighter.SetPixelDirect(x, y, (byte)nRed, (byte)nGreen, (byte)nBlue);
                    bitmapDarker.SetPixelDirect(x, y, (byte)(r - m_nRedDiff), (byte)(g - m_nGreenDiff), (byte)(b - m_nBlueDiff));
                }
            }

            // r g b 
            bitmapMoreRed.UnlockImage();
            pictureBoxMoreRed.BackgroundImage = bitmapMoreRed.InternalBitmap;

            bitmapMoreGreen.UnlockImage();
            pictureBoxMoreGreen.BackgroundImage = bitmapMoreGreen.InternalBitmap;

            bitmapMoreBlue.UnlockImage();
            pictureBoxMoreBlue.BackgroundImage = bitmapMoreBlue.InternalBitmap;

            //cyan yellow magenta
            bitmapMoreCyan.UnlockImage();
            pictureBoxMoreCyan.BackgroundImage = bitmapMoreCyan.InternalBitmap;

            bitmapMoreYellow.UnlockImage();
            pictureBoxMoreYellow.BackgroundImage = bitmapMoreYellow.InternalBitmap;

            bitmapMoreMagenta.UnlockImage();
            pictureBoxMoreMagenta.BackgroundImage = bitmapMoreMagenta.InternalBitmap;

            //lighter darker
            bitmapLighter.UnlockImage();
            pictureBoxLighter.BackgroundImage = bitmapLighter.InternalBitmap;

            bitmapDarker.UnlockImage();
            pictureBoxDarker.BackgroundImage = bitmapDarker.InternalBitmap;

            bitmap.UnlockImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkbShowClipping_CheckedChanged(object sender, EventArgs e)
        {
            ShowAllVariations();
        }

        /// <summary>
        /// Shows all Variations
        /// </summary>
        private void ShowAllVariations()
        {
            UnsafeBitmap bitmap = (Bitmap)m_ImageCurrentPick.Clone();
            AllVariations(bitmap);
            bitmap.UnlockImage();
        }
    }
}
